home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / gms_joyports.lha / JoyPorts / JOY_Init.c next >
Text File  |  1998-10-10  |  12KB  |  413 lines

  1.  
  2. #define JP_MOUSE    0
  3. #define JP_JOYSTICK 1
  4. #define JP_ANALOGUE 2
  5. #define JP_JOYPAD   3
  6. #define JP_SEGAPAD  4
  7. #define JP_KEYBOARD 5
  8.  
  9. void ReadAnalogue(struct JoyData *JoyData);
  10. void ReadJoyPad(struct JoyData *JoyData);
  11. void ReadSegaPad(struct JoyData *JoyData);
  12. void ReadJoystick(struct JoyData *JoyData);
  13. void ReadKeyboard(struct JoyData *JoyData);
  14. void ReadMouse(struct JoyData *JoyData);
  15.  
  16. /***********************************************************************************/
  17.  
  18. void ReadAnalogue(struct JoyData *JoyData)
  19. {
  20.   ReadMouse(JoyData);
  21. }
  22.  
  23. /***********************************************************************************/
  24.  
  25. void ReadJoyPad(struct JoyData *JoyData)
  26. {
  27.   ReadJoystick(JoyData);
  28. }
  29.  
  30. /***********************************************************************************/
  31.  
  32. void ReadSegaPad(struct JoyData *JoyData)
  33. {
  34.   ReadJoystick(JoyData);
  35. }
  36.  
  37. /************************************************************************************
  38. ** This is the joystick emulator for the Keyboard.  It uses the Keyboard object
  39. ** to read the keys and compares them to the emulation keys set out by the user.
  40. ** I guess it is kind of slower than what might be preferred, but it is an emulator
  41. ** after all.
  42. **
  43. ** BUG/HACK - Using this method the program will actually interfere with the Task's
  44. ** use of the keyboard, so we should allow for a qualifier key from GMSPrefs
  45. ** and also prevent the key buffer position from moving.
  46. */
  47.  
  48. void ReadKeyboard(struct JoyData *JoyData)
  49. {
  50.    struct Keyboard *keys;
  51.    struct JoyKeys  *emu;
  52.    WORD  i;
  53.    WORD  qual;
  54.    UBYTE value;
  55.  
  56.    JoyData->XChange = NULL;
  57.    JoyData->YChange = NULL;
  58.    JoyData->ZChange = NULL;
  59.    JoyData->Buttons = NULL;
  60.  
  61.    if ((keys = JoyData->prvKeys) AND (emu = JoyData->prvEmu)) {
  62.       Query(keys);
  63.  
  64.       for (i = 0; i < keys->AmtRead; i++) {
  65.          qual  = keys->Buffer[i].Qualifier;
  66.  
  67.          if (qual & KQ_HELD) {
  68.             value = keys->Buffer[i].Value;
  69.  
  70.                  if (value IS emu->Left)  JoyData->XChange -= 1;
  71.             else if (value IS emu->Right) JoyData->XChange += 1;
  72.             else if (value IS emu->Up)    JoyData->YChange -= 1;
  73.             else if (value IS emu->Down)  JoyData->YChange += 1;
  74.             else if (value IS emu->Fire1) JoyData->Buttons |= JD_FIRE1;
  75.             else if (value IS emu->Fire2) JoyData->Buttons |= JD_FIRE2;
  76.             else if (value IS emu->Fire3) JoyData->Buttons |= JD_FIRE3;
  77.             else if (value IS emu->Fire4) JoyData->Buttons |= JD_FIRE4;
  78.             else if (value IS emu->Fire5) JoyData->Buttons |= JD_FIRE5;
  79.             else if (value IS emu->Fire6) JoyData->Buttons |= JD_FIRE6;
  80.             else if (value IS emu->Fire7) JoyData->Buttons |= JD_FIRE7;
  81.             else if (value IS emu->Fire8) JoyData->Buttons |= JD_FIRE8;
  82.          }
  83.       }
  84.    }
  85. }
  86.  
  87. /***********************************************************************************/
  88.  
  89. void ReadMouse(struct JoyData *JoyData)
  90. {
  91.    UWORD tmp;
  92.    WORD  X, Y;
  93.    LONG  buttons = NULL;
  94.  
  95.    /*** PORT 1 ***/
  96.  
  97.    if (JoyData->Port IS 1) {
  98.       tmp = custom->joy0dat;
  99.       X   = tmp & 0x00ff;
  100.       Y   = (tmp>>8);
  101.  
  102.       tmp = JoyData->prvOldX;
  103.       JoyData->prvOldX = X;
  104.       X -= tmp;
  105.  
  106.       tmp = JoyData->prvOldY;
  107.       JoyData->prvOldY = Y;
  108.       Y -= tmp;
  109.  
  110.       if (!(X > -127)) { X += 255; }
  111.       if (!(X < +127)) { X -= 255; }
  112.       if (!(Y > -127)) { Y += 255; }
  113.       if (!(Y < +127)) { Y -= 255; }
  114.  
  115.       tmp = custom->potinp;
  116.       if (!(cia->ciapra & (1<<6))) buttons |= JD_LMB;
  117.       if (!(tmp & (1<<10)))        buttons |= JD_RMB;
  118.       if (!(tmp & (1<<12)))        buttons |= JD_MMB; /* This is a guess... */
  119.    }
  120.  
  121.    /*** PORT 2 ***/
  122.  
  123.    else if (JoyData->Port IS 2) {
  124.       tmp = custom->joy1dat;
  125.       X   = tmp & 0x00ff;
  126.       Y   = (tmp>>8);
  127.       tmp = JoyData->prvOldX;
  128.       JoyData->prvOldX = X;
  129.       X  -= tmp;
  130.       tmp = JoyData->prvOldY;
  131.       JoyData->prvOldY = Y;
  132.       Y  -= tmp;
  133.  
  134.       if (!(X > -127)) { X += 255; }
  135.       if (!(X < +127)) { X -= 255; }
  136.       if (!(Y > -127)) { Y += 255; }
  137.       if (!(Y < +127)) { Y -= 255; }
  138.  
  139.       tmp = custom->potinp;
  140.       if (!(cia->ciapra & (1<<7))) buttons |= JD_LMB;
  141.       if (!(tmp & (1<<14)))        buttons |= JD_RMB; /* WRONG */
  142.       if (!(tmp & (1<<8)))         buttons |= JD_MMB; /* This is a guess... */
  143.    }
  144.  
  145.    /*** UNSUPPORTED PORT ***/
  146.  
  147.    else {
  148.       JoyData->XChange = NULL;
  149.       JoyData->YChange = NULL;
  150.       JoyData->ZChange = NULL;
  151.       JoyData->Buttons = NULL;
  152.       return;
  153.    }
  154.  
  155.    JoyData->XChange = X;
  156.    JoyData->YChange = Y;
  157.    JoyData->ZChange = NULL;
  158.    JoyData->Buttons = buttons;
  159. }
  160.  
  161. /***********************************************************************************/
  162.  
  163. void ReadJoystick(struct JoyData *JoyData)
  164. {
  165.    UWORD joy;
  166.    UWORD tmp;
  167.    LONG  buttons = NULL;
  168.  
  169.    tmp = custom->potinp;
  170.  
  171.    /*** PORT 1 ***/
  172.  
  173.    if (JoyData->Port IS 1) {
  174.       joy = custom->joy0dat;
  175.       if (!(cia->ciapra & (1<<6))) buttons |= JD_FIRE1;
  176.       if (!(tmp & (1<<10)))        buttons |= JD_FIRE2; /* WRONG */
  177.       if (!(tmp & (1<<14)))        buttons |= JD_FIRE3; /* This is a guess... */
  178.    }
  179.  
  180.    /*** PORT 2 ***/
  181.  
  182.    else if (JoyData->Port IS 2) {
  183.       joy = custom->joy1dat;
  184.       if (!(cia->ciapra & (1<<7))) buttons |= JD_FIRE1;
  185.       /*if (!(tmp & (1<<14)))*/    buttons |= JD_FIRE2; /* WRONG */
  186.       if (!(tmp & (1<<8)))         buttons |= JD_FIRE3; /* This is a guess... */
  187.    }
  188.  
  189.    /*** UNSUPPORTED PORT ***/
  190.  
  191.    else {
  192.       JoyData->XChange = NULL;
  193.       JoyData->YChange = NULL;
  194.       JoyData->ZChange = NULL;
  195.       JoyData->Buttons = NULL;
  196.       return;
  197.    }
  198.  
  199.    if (joy & 0x0002)      JoyData->XChange = 1;
  200.    else if (joy & 0x0200) JoyData->XChange = -1;
  201.    else JoyData->XChange = NULL;
  202.  
  203.    if ((joy >> 1^ joy) & 0x0001)      JoyData->YChange = 1;
  204.    else if ((joy >> 1^ joy) & 0x0100) JoyData->YChange = -1;
  205.    else JoyData->YChange = NULL;
  206.  
  207.    JoyData->Buttons = buttons;
  208. }
  209.  
  210. /************************************************************************************
  211. ** Action: Free()
  212. ** Object: JoyData
  213. */
  214.  
  215. LIBFUNC void JOY_Free(mreg(__a0) struct JoyData *JoyData)
  216. {
  217.   if (JoyData->prvKeys) {
  218.      Free(JoyData->prvKeys);
  219.   }
  220.   Public->OpenCount--;
  221. }
  222.  
  223. /************************************************************************************
  224. ** Action: Get()
  225. ** Object: JoyData
  226. */
  227.  
  228. LIBFUNC struct JoyData * JOY_Get(mreg(__a0) struct Stats *Stats)
  229. {
  230.   struct JoyData *JoyData;
  231.  
  232.   if (JoyData = AllocMemBlock(sizeof(struct JoyData), MEM_RESOURCED|Stats->MemFlags)) {
  233.      JoyData->Head.ID      = ID_JOYDATA;
  234.      JoyData->Head.Version = VER_JOYDATA;
  235.      Public->OpenCount++;
  236.      return(JoyData);
  237.   }
  238.   else {
  239.      ErrCode(ERR_FAILED);
  240.      return(NULL);
  241.   }
  242. }
  243.  
  244. /************************************************************************************
  245. ** Action: Init()
  246. ** Object: JoyData
  247. **
  248. ** Resets the timer and all of the values in the structure.
  249. */
  250.  
  251. LIBFUNC ECODE JOY_Init(mreg(__a0) struct JoyData *JoyData)
  252. {
  253.   struct DPKTask *Task;
  254.  
  255.   /* Note that we set mouse port as the system default if the
  256.   ** programmer has not set a port number.
  257.   */
  258.  
  259.   if (JoyData->Port IS JPORT_ANALOGUE) {
  260.      JoyData->Port = 1;
  261.   }
  262.   else if (JoyData->Port IS JPORT_DIGITAL) {
  263.      JoyData->Port = 2;
  264.   }
  265.   else if (JoyData->Port IS NULL) {
  266.      JoyData->Port = 1;
  267.   }
  268.   else if ((JoyData->Port < 1) OR (JoyData->Port > 4)) {
  269.      DPrintF("!Init:","JoyData port number is out of range (1 - 4).");
  270.      return(ERR_DATA);
  271.   }
  272.  
  273.   /* Set time-out values (micro-seconds) */
  274.  
  275.   if (JoyData->ButtonTimeOut IS NULL) {
  276.      JoyData->ButtonTimeOut = 200;
  277.   }
  278.  
  279.   if (JoyData->MoveTimeOut IS NULL) {
  280.      JoyData->MoveTimeOut = 200;
  281.   }
  282.  
  283.   /* Set the limits.  We also check that the sign of each setting
  284.   ** is correct.
  285.   */
  286.  
  287.   if (JoyData->NXLimit IS NULL) JoyData->NXLimit = -100;
  288.   if (JoyData->NYLimit IS NULL) JoyData->NYLimit = -100;
  289.   if (JoyData->PXLimit IS NULL) JoyData->PXLimit = +100;
  290.   if (JoyData->PYLimit IS NULL) JoyData->PYLimit = +100;
  291.  
  292.   if (JoyData->NXLimit > 0) JoyData->NXLimit = -JoyData->NXLimit;
  293.   if (JoyData->NYLimit > 0) JoyData->NYLimit = -JoyData->NYLimit;
  294.   if (JoyData->PXLimit < 0) JoyData->PXLimit = -JoyData->PXLimit;
  295.   if (JoyData->PYLimit < 0) JoyData->PYLimit = -JoyD